package com.yifeng.repo.tookit.oss.config;

import com.yifeng.repo.tookit.oss.OssFileClient;
import com.yifeng.repo.tookit.oss.web.OssServletRegistrar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {
    private final OssProperties ossProperties;

    public OssAutoConfiguration(OssProperties ossProperties) {
        this.ossProperties = ossProperties;
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.toolkit.oss", name = "enable", havingValue = "true")
    public OssFileClient ossFileClient() {
        OssFileClient ossFileClient = new OssFileClient(
                ossProperties.getAppId(),
                ossProperties.getUrl(),
                ossProperties.getMerchantId(),
                ossProperties.getAccessToken(),
                ossProperties.getTmpFilePath()
        );
        log.info("**** OssFileClient init success ****");
        return ossFileClient;
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.toolkit.oss", name = "enable-admin-api")
    public OssServletRegistrar ossServletRegistrar(OssFileClient ossFileClient) {
        return new OssServletRegistrar(ossProperties.getAdminApiPrefix(), ossFileClient);
    }
}
